package DataBaseTest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Delete extends JInternalFrame {
	Connection conn;
	DataBaseTest_0528 main;
	private JLabel label;
	private JTextField code;
	private JButton button;
	private JLabel label_1;
	private JTextField ea;
	private JLabel label_2;
	private JTextField serial;
	private JButton button_1;
	private JLabel status;
	

	public Delete(Connection c,DataBaseTest_0528 main) {
		super("삭제",true,true,true,true);
		this.main=main;
		getContentPane().setBackground(SystemColor.inactiveCaption);
		setMaximizable(true);
		setClosable(true);
		setTitle("삭제");
		setBounds(100, 100, 311, 238);
		this.conn=c;
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getCode());
		getContentPane().add(getButton());
		getContentPane().add(getLabel_1());
		getContentPane().add(getEa());
		getContentPane().add(getLabel_2());
		getContentPane().add(getSerial());
		getContentPane().add(getButton_1());
		getContentPane().add(getStatus());
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("코드 ");
			label.setFont(new Font("서울남산체 L", Font.PLAIN, 13));
			label.setForeground(Color.WHITE);
			label.setBounds(12, 79, 57, 15);
		}
		return label;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(85, 76, 116, 21);
		}
		return code;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("검 색");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s=Integer.parseInt(serial.getText());
					String sql="select*from ptbl where serial=?";
					try {
						PreparedStatement pstmt =
								conn.prepareStatement(sql);
						pstmt.setInt(1,s);
						
						ResultSet rs= pstmt.executeQuery();
						if(rs.next()){
							code.setText(rs.getString("code"));
							ea.setText(""+rs.getInt("ea"));
						}else{
							status.setText("데이터가 없습니다.");
							code.setText("");
							ea.setText("");
							serial.requestFocus();
						}
							rs.close();
							pstmt.close();
							
						} catch (Exception e2) {
							e2.printStackTrace();
						}
				}
			});
			button.setForeground(Color.WHITE);
			button.setFont(new Font("서울남산체 L", Font.PLAIN, 13));
			button.setBackground(SystemColor.activeCaption);
			button.setBounds(213, 10, 71, 23);
		}
		return button;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("EA");
			label_1.setFont(new Font("서울남산체 L", Font.PLAIN, 13));
			label_1.setForeground(Color.WHITE);
			label_1.setBounds(12, 119, 57, 15);
		}
		return label_1;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(85, 116, 116, 21);
		}
		return ea;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("SERIAL");
			label_2.setFont(new Font("서울남산체 L", Font.PLAIN, 13));
			label_2.setForeground(Color.WHITE);
			label_2.setBounds(12, 13, 57, 15);
		}
		return label_2;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(85, 10, 116, 21);
		}
		return serial;
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("삭제");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s= Integer.parseInt(serial.getText());
					String sql ="delete from ptbl where serial=?";
					try {
						PreparedStatement pstmt 
							= conn.prepareStatement(sql);
						
						pstmt.setInt(1, s);
						pstmt.executeUpdate();
						
						pstmt.close();
						
						status.setText("데이터가 삭제되었습니다.");
						code.setText("");
						ea.setText("");
						serial.requestFocus();
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			});
			button_1.setForeground(Color.WHITE);
			button_1.setFont(new Font("서울남산체 L", Font.PLAIN, 13));
			button_1.setBackground(SystemColor.activeCaption);
			button_1.setBounds(85, 147, 116, 23);
		}
		return button_1;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("New label");
			status.setForeground(SystemColor.window);
			status.setFont(new Font("서울남산체 L", Font.PLAIN, 13));
			status.setBounds(12, 180, 272, 15);
		}
		return status;
	}
}
